import { RouteRecordRaw } from 'vue-router'

import { configSetting, type GlobalConfig } from '@/config'

import { defaultConfig } from './type'

const homeConfig = {
  path: 'home',
  name: configSetting.HomeName,
  meta: {
    title: '首页',
    icon: 'icon-shouye'
  }
}

interface RouterConfigInter {
  aloneRouter: RouteRecordRaw
  multiRouter: RouteRecordRaw
}

const routerConfig = defaultConfig<RouterConfigInter>({
  aloneRouter: {
    path: '/',
    component: () => import('@/layout/ComponentLayout.vue'),
    name: configSetting.MianLayoutName,
    meta: {},
    redirect: { name: configSetting.HomeName },
    children: [
      {
        ...homeConfig,
        component: () => import('@/container/home/home.vue')
      }
    ]
  },
  multiRouter: {
    path: '/',
    component: () => import('@/layout/layout-shelf/HomeLayout.vue'),
    name: configSetting.MianLayoutName,
    meta: {},
    redirect: { name: configSetting.HomeName },
    children: [
      {
        ...homeConfig,
        component: () =>
          import('@/container/multi-module/multi-module-home.vue')
      }
    ]
  }
})

class MergeRoutes {
  routesConfig: GlobalConfig
  constructor(config: GlobalConfig) {
    this.routesConfig = config
  }
  setMergeRoutes(routes: RouteRecordRaw[]) {
    const { mergeRouter } = this.routesConfig
    if (mergeRouter) {
      return [routerConfig.aloneRouter, ...routes]
    }
    return [routerConfig.multiRouter, ...routes]
  }
}

export default MergeRoutes
